/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package apus.action.account;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import apus.action.Action;
import apus.action.ActionResult;
import apus.entity.Account;
import apus.entity.RealAccount;
import apus.persistence.AccountDAO;
import apus.services.ServiceLocator;

/**
 *
 * @author Admin
 */
public class AccountDeleteAction implements Action {

    @Override
    public ActionResult execute(HttpServletRequest request, HttpServletResponse response) {
        if (request.getParameter("id") != null) {
            int id = Integer.parseInt(request.getParameter("id"));
            Account account = new RealAccount();
            account.setId(id);
            ServiceLocator.getService(AccountDAO.class).delete(account);
            request.getSession().setAttribute("win", "Account removed");
        } else {
            request.getSession().setAttribute("fail", "Account was not removed");
        }
        List<Account> accountList = ServiceLocator.getService(AccountDAO.class).findAll();
        request.setAttribute("accountList", accountList);
        return ActionResult.redirect("account.list");
    }

    @Override
    public String getName() {
        return "account.delete";
    }

}